package sy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sy.pageModel.BaseEntity;
import sy.pageModel.DataGrid;
import sy.service.ActivityServiceI;
import sy.util.StringUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016-07-12.
 */

@Service
public class ActivityServiceImpl implements ActivityServiceI {

    @Autowired
    private JdbcTemplate template;

    @Override
    public DataGrid findBaseEntityDataGird(BaseEntity currency) {
        DataGrid dg = new DataGrid();
        String sql = "select count(*) from activity_time where 1=1 ";
        sql = addWhere(currency, sql);//模糊查询
        long total = template.queryForLong(sql);
        dg.setTotal(total);//获取总数
        sql = "SELECT *from activity_time where 1=1  ";
        sql = addWhere(currency, sql);
        sql = addOrderBy(currency, sql);
        sql = addLimit(currency, sql);
        final List<BaseEntity> currencyList = new ArrayList<BaseEntity>();
        template.query(sql, new RowCallbackHandler(){
            public void processRow(ResultSet rs) throws SQLException {
                BaseEntity curr=new BaseEntity();
                curr.setId(rs.getString("id"));
                curr.setDay(rs.getInt("day"));
                curr.setCreateTimeStart(rs.getString("start_time"));
                curr.setCreateTimeEnd(rs.getString("end_time"));
                curr.setIsShow(rs.getString("is_use"));
                curr.setText(rs.getString("creat_time"));
                currencyList.add(curr);
            }});
        dg.setRows(currencyList);
        return dg;
    }

    @Override
    @Transactional
    public int addActivityService(BaseEntity curr) {
        int rows=0;
        //先修改后添加
        String sql="UPDATE activity_time set is_use=? ";
        rows= template.update(sql,new Object[]{"N"});
        sql="INSERT INTO activity_time(day, start_time, end_time, is_use, creat_time) values (?,?,?,?,?)";
        rows=template.update(sql,new Object[]{curr.getDay(),curr.getCreateTimeStart(),curr.getCreateTimeEnd(),"Y", StringUtil.getDate()});
        return rows;
    }

    @Override
    public int editActivityService(BaseEntity curr) {
        int rows=0;
        String sql="UPDATE activity_time set is_use=? ";
        rows= template.update(sql,new Object[]{"N"});
        sql="UPDATE activity_time SET  is_use=? where id=?";
        rows=template.update(sql,new Object[]{"Y",curr.getId()});
        return rows;
    }

    private String addLimit(BaseEntity currency, String sql) {
        int fromRow = (currency.getPage() - 1) * currency.getRows();
        sql += " limit " + fromRow + "," + currency.getRows();
        return sql;
    }

    private String addWhere(BaseEntity currency, String sql) {
//        if(!StringUtil.isNullOrEmpty(currency.getTypeId())) sql += " and typeid = '" + currency.getTypeId() + "'";
        return sql;
    }

    private String addOrderBy(BaseEntity currency, String sql) {
        if (currency.getSort() != null) {
            if("isShow".equals(currency.getSort())) currency.setSort("is_use");
            sql += " order by " + currency.getSort() + " " + currency.getOrder();
        }
        return sql;
    }

}
